<?php

namespace App\Admin\Form;

use App\Admin\Admin;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Collection;

class Tools implements Renderable
{
    /**
     * 表单构造器
     *
     * @var Builder
     */
    protected $form;

    /**
     * 工具集
     *
     * @var Collection
     */
    protected $tools;

    /**
     * 初始设置
     *
     * @var array
     */
    protected $options = [
        'enableListButton'   => true,
        'enableBackButton'   => true,
        'enableDeleteButton' => false,
    ];

    /**
     * Tool constructor.
     * @param Builder $builder
     */
    public function __construct(Builder $builder)
    {
        $this->form = $builder;

        $this->tools = new Collection();
    }

    /**
     * history返回按钮
     *
     * @return string
     */
    protected function backButton()
    {
        return '<a class="layui-btn layui-btn-sm form-history-back">返回</a>';
    }

    /**
     * 返回列表按钮
     *
     * @return string
     */
    protected function listButton()
    {
        $resource = $this->form->getResource();

        return "<a href='$resource' class='layui-btn layui-btn-sm'>列表</a>";
    }

    protected function deleteButton()
    {
        $resourceId = $this->form->getResourceId();

        return "<button type='button' class='layui-btn layui-btn-sm layui-btn-danger item-delete' data-id='{$resourceId}'>删除</button>";
    }

    /**
     * 添加自定义按钮
     *
     * @param $tool
     * @return $this
     */
    public function add($tool)
    {
        $this->tools->push($tool);

        return $this;
    }

    /**
     * 禁用返回按钮
     *
     * @return $this
     */
    public function disableBackButton()
    {
        $this->options['enableBackButton'] = false;

        return $this;
    }

    /**
     * 禁用返回列表按钮
     *
     * @return $this
     */
    public function disableListButton()
    {
        $this->options['enableListButton'] = false;

        return $this;
    }

    /**
     * 开启表单中的删除按钮
     *
     * @return $this
     */
    public function enableDeleteButton()
    {
        $this->options['enableDeleteButton'] = true;

        return $this;
    }

    /**
     * 生成工具的html字符串
     *
     * @return string
     */
    public function render()
    {
        if ($this->options['enableBackButton']) {
            $this->add($this->backButton());
        }

        if ($this->options['enableListButton']) {
            $this->add($this->listButton());
        }

        if ($this->options['enableDeleteButton'] && !$this->form->isMode(Builder::MODE_CREATE)) {
            $this->add($this->deleteButton());

            $confirm = '确认删除？';
            $token = csrf_token();
            $resource = $this->form->getResource();
            $script = <<<SCRIPT

            $('.item-delete').click(function() {
                var id = $(this).data('id');
                
                layer.confirm('{$confirm}', {icon: 0, title:'警告'}, function (index) {
                    $.post('{$resource}/' + id, {_method:'delete','_token':'{$token}'}, function(data){
                        if (data.status) {
                            location.href = '{$resource}';
                        } else {
                            layer.msg('删除失败,请刷新后重试');
                        }
                    });
                    
                    layer.close(index);
                });
            });

SCRIPT;

            Admin::script($script);
        }

        return $this->tools->map(function ($tool) {
            if ($tool instanceof Renderable) {
                return $tool->render();
            }

            if ($tool instanceof Htmlable) {
                return $tool->toHtml();
            }

            return (string)$tool;
        })->implode(' ');
    }
}